/**
 * Created by gongxun on 16/11/20.
 */

var KwxRecordInfoLayer = KwxSecondBase.extend({
    PlayList : null,
    ctor : function (playList) {
        this._super();
        this.loadSecondCCS("res/ccs/KwxRecordInfoLayer.json");
        this.setTitleText("res/ccs/hall_second/text/text_lishizhanjixiangxi.png");

        this.PlayList = playList;
        this.PlayList.items.sort(function(a, b){
            return a.BeginTime - b.BeginTime;
        });

        this.ListView_content = mo.seekWidgetByName(this.ccsNode, "ListView_content");
        this.ListView_content.setScrollBarEnabled(false);

        //查看其他人的录像
        mo.seekWidgetByName(this.ccsNode, "Button_ckzj").addTouchEventListener(function(sender, type){
            if (mo.gameHelper.getIsTouchEndMenuOpen(type)) {
                mo.mainLayer.addChild(new KwxRecordOtherLayer());
            }
        }, this);

        //提示信息
        this.setTishiInfo();

        this.loadItemTemple();

        //修复剪切bug
        mo.gameHelper.setClippingLayout(this.ListView_content);
    },

    onEnter : function(){
        this._super();

        var that = this;
        this.scheduleOnce(function(){
            mo.closeWaitLayer();
            that.setPlayList();
        }, 0.5);

        mo.eventHelper.add("RePlayResponse", this, function(RePlayResponse){
            var index = 0;
            for (var i = 0; i < this.PlayList.items.length; i++){
                if (this.PlayList.items[i].ReplayId == RePlayResponse.ReplayId){
                    index = i;
                    break;
                }
            }
            var ui = this.ListView_content.getItem(index);
            var Button_play = mo.seekWidgetByName(ui, "Button_play");
            Button_play.loadTextureNormal("res/ccs/hall_second/record/btn_chakan.png");
            this.setButtonTag(Button_play, 2);
        });
    },

    onExit : function(){
        mo.eventHelper.remove(this);
        this._super();
    },

    loadItemTemple : function(){
        var ui = mo.seekWidgetByName(this.ccsNode, "Panel_temple");
        this.ListView_content.setItemModel(ui);
        this.ListView_content.removeAllChildren();
    },

    addListViewItem : function(){
        this.ListView_content.pushBackDefaultItem();
        var ui = this.ListView_content.getItem(this.ListView_content.getItems().length - 1);
        var size = cc.size(this.ListView_content.getContentSize().width, 130);

        ui.setContentSize(size);
        var height = mo.gameHelper.projectCode == 2 ? 120 : 130;
        mo.seekWidgetByName(ui, "Image_bg").setContentSize(cc.size(size.width, height));
        mo.seekWidgetByName(ui, "Button_play").setPositionX(size.width - 20);
        mo.seekWidgetByName(ui, "Button_share").setPositionX(size.width - 20);

        return ui;
    },

    setPlayList : function(){

        var loseColor = cc.color(40, 62, 144);
        var winColor = cc.color(182, 113, 0);

        for(var i = 0; i < this.PlayList.items.length; i++){
            var playInfo = this.PlayList.items[i];

            //ui
            var ui = this.addListViewItem();
            var Button_play = mo.seekWidgetByName(ui, "Button_play");
            Button_play.setTag(i);
            Button_play.addTouchEventListener(this.onTouchPlay, this);

            var Button_share = mo.seekWidgetByName(ui, "Button_share");
            Button_share.setTag(i);
            Button_share.addTouchEventListener(this.onTouchShare, this);

            //time
            var date = new Date(playInfo.BeginTime * 1000);
            var time =
                mo.gameHelper.getFullString((date.getMonth() + 1), "0", 2) +
                "-" +
                mo.gameHelper.getFullString((date.getDate()), "0", 2) +
                "\n" +
                mo.gameHelper.getFullString((date.getHours()), "0", 2) +
                ":" +
                mo.gameHelper.getFullString((date.getMinutes()), "0", 2) +
                ":" +
                mo.gameHelper.getFullString((date.getSeconds()), "0", 2);
            mo.gameHelper.setTextString(mo.seekWidgetByName(ui, "Text_time"), time);

            var nicks = playInfo.Nick;
            var uids = playInfo.Uid;
            var faceUrls = playInfo.FaceUrl;
            var scores = playInfo.Score;
            var mySeat = 0;
            for (var seat = 0; seat < uids.length; seat++){
                if (uids[seat] == mo.gameUserInfo.uid){
                    mySeat = seat;
                    break;
                }
            }

            //游戏类型
            var gameId = playInfo.GameId;
            var Panel_gameType = null;

            if (gameId == 14 || gameId == 15){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(true);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(false);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeKwx");
            }else if (gameId == 9){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");
            }else if (gameId == 21){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");
            }else if (gameId == 22){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");
            }else if (gameId == 20){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");
            }else if (gameId == 23){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");
            }else if (gameId == 25){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(false);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(true);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeNn");
            }else if (gameId == 26){
                mo.seekWidgetByName(ui, "Panel_gameTypeKwx").setVisible(true);
                mo.seekWidgetByName(ui, "Panel_gameTypeNn").setVisible(false);
                Panel_gameType = mo.seekWidgetByName(ui, "Panel_gameTypeKwx");
            }

            //nick score
            for (var j = 0; j < uids.length; j++){
                var Panel_player = mo.seekWidgetByName(Panel_gameType, "Panel_player" + j);
                Panel_player.setVisible(true);
                var getSeat = (j + mySeat) % uids.length;

                var Text_player = mo.seekWidgetByName(Panel_player, "Text_player0");
                mo.gameHelper.setTextString(Text_player, mo.gameHelper.getUserNickFilterForCount(nicks[getSeat], 4));

                var Text_score = mo.seekWidgetByName(Panel_player, "Text_score0");
                mo.gameHelper.setTextString(Text_score, scores[getSeat] + "");
                if (gameId == 9 || gameId == 21 || gameId == 22 || gameId == 20 || gameId == 23 || gameId == 25){
                    Text_score.setPositionX(25 + Text_player.getContentSize().width);
                }

                Text_score.setTextColor(scores[getSeat] >= 0 ? winColor : loseColor);

                //head
                mo.gameHelper.loadImageForUrl(mo.seekWidgetByName(Panel_player, "Image_head"), faceUrls[getSeat]);
            }

            //录像能否播放
            var isExit = mo.recordDetailedHelper.getRecordDetailed(playInfo.ReplayId);
            if (isExit){
                Button_play.loadTextureNormal("res/ccs/hall_second/record/btn_chakan.png");
                this.setButtonTag(Button_play, 2);
            }else{
                Button_play.loadTextureNormal("res/ccs/hall_second/record/btn_down.png");
                this.setButtonTag(Button_play, 0);
            }
        }
    },

    setButtonTag : function(sender, tag){
        mo.seekWidgetByName(sender, "Panel_tag").setTag(tag);
    },

    getButtonTag : function(sender){
        return mo.seekWidgetByName(sender, "Panel_tag").getTag();
    },

    onTouchPlay : function(sender, type){
        if (mo.gameHelper.getIsTouchEnd(type)) {
            var index = sender.getTag();
            var playInfo = this.PlayList.items[index];

            var tag = this.getButtonTag(sender);
            if (tag == 0){
                mo.gameMsgManager.sendRePlayRequest(playInfo.ReplayId);
            }else if (tag == 1){

            }else if (tag == 2){
                mo.recordDetailedHelper.playReplayForId(playInfo.ReplayId);
                cc.log("replay ok");
            }
        }
    },

    onTouchShare : function(sender, type){
        if (mo.gameHelper.getIsTouchEnd(type)) {
            var index = sender.getTag();
            var playInfo = this.PlayList.items[index];
            var description = "我在" + mo.gameHelper.projectName + "卡五星打了一把游戏，在游戏中输入录像id就能看到我的精彩回放了";
            if (playInfo.GameId == 14 || playInfo.GameId == 15){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-卡五星-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }else if (playInfo.GameId == 9){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-牛牛-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }else if (playInfo.GameId == 21){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-推筒子-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }else if (playInfo.GameId == 22){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-拼三张-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }else if (playInfo.GameId == 20){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-武汉麻将-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }else if (playInfo.GameId == 23){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-德州扑克-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }else if (playInfo.GameId == 25){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-拼天九-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }else if (playInfo.GameId == 26){
                var title = "录像id:" + playInfo.ReplayId.substr(7) + "-跑得快-游戏录像";
                mo.gameHelper.onWxShareWeb(title, description, mo.gameUserInfo.getExternalUrl(2, playInfo.ReplayId.substr(7)));
            }
        }
    },

    setTishiInfo : function(){
        var Text_tishi = mo.seekWidgetByName(this.ccsNode, "Text_tishi");
        if (this.PlayList.items.length == 0){
            Text_tishi.setString("");
            return;
        }

        var startTime = this.PlayList.items[0].EndTime;
        var endTime = this.PlayList.items[this.PlayList.items.length - 1].BeginTime;

        var getTimeString = function(time){
            var date = new Date(time * 1000);
            var time = mo.gameHelper.getFullString((date.getMonth() + 1), "0", 2) + "月" + mo.gameHelper.getFullString((date.getDate()), "0", 2) + "日" + mo.gameHelper.getFullString((date.getHours()), "0", 2) + ":" + mo.gameHelper.getFullString((date.getMinutes()), "0", 2);
            return time;
        };

        var tishiString = getTimeString(startTime) + " - " + getTimeString(endTime);
        tishiString += "   " + this.PlayList.items.length + "盘战绩详细";

        mo.gameHelper.setTextString(Text_tishi, tishiString);
    }
});